﻿
using FB.Membership;
using FB.Membership.Providers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

namespace FB.Membership
{
    public class PermissionManager 
    {

        public static List<Permission> GetUserPermissions(MembershipUser user)
        {
            return AspNetPermissionManager.Default.GetUserPermissions(user);
        }

        public static List<Permission> GetUserPermissions(MembershipUser user, bool includeRole)
        {
            return AspNetPermissionManager.Default.GetUserPermissions(user, includeRole);
        }

        public static List<Permission> GetRolePermissions(string roleName)
        {
            return AspNetPermissionManager.Default.GetRolePermissions(roleName);
        }

        public static List<Permission> GetRolesPermissions(params string[] rolesName)
        {
            return AspNetPermissionManager.Default.GetRolesPermissions(rolesName);
        }

        public static List<string> FindUserHasPermission<P>(P perm)
        {
            return AspNetPermissionManager.Default.FindUserHasPermission<P>(perm);
        }

        public static bool Validate<P>(List<Permission> permList, P perm)
        {
            return AspNetPermissionManager.Default.Validate(permList, perm);
        }
        public static bool ValidateModule<M>(List<Permission> permList, M module)
        {
            return AspNetPermissionManager.Default.ValidateModule(permList, module);
        }

        public static bool AnyOf<P>(List<Permission> permList)
        {
            return AspNetPermissionManager.Default.AnyOf<P>(permList);
        }



        public static void AddToUser<P>(MembershipUser user, P permFlag) where P : struct
        {
            AspNetPermissionManager.Default.AddToUser(user, permFlag);
        }

        public static void AddToRole<P>(string role, P permFlag) where P : struct
        {
            AspNetPermissionManager.Default.AddToRole(role, permFlag);
        }

        public static void RemoveFromUser<P>(MembershipUser user, P permFlag) where P : struct
        {
            AspNetPermissionManager.Default.RemoveFromUser(user, permFlag);
        }

        public static void RemoveFromRole<P>(string role, P permFlag) where P : struct
        {
            AspNetPermissionManager.Default.RemoveFromRole(role, permFlag);
        }


        public static List<Permission> PermissionSession
        {
            get { return System.Web.HttpContext.Current.Session[string.Format("{0}-$PERM", FB.Helpers.AppSettingsBase.AppKey)] as List<Permission>; }
            set { System.Web.HttpContext.Current.Session.Add(string.Format("{0}-$PERM", FB.Helpers.AppSettingsBase.AppKey), value); }
        }
    }
}
